package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.CCPT;
import com.zretc.emptraining.service.CCPTService;
import com.zretc.emptraining.service.RefTableService;
import com.zretc.emptraining.serviceimpl.CCPTServiceImpl;
import com.zretc.emptraining.serviceimpl.RefTableServiceImp;

/**
 * 课程、课程计划、教员 联结查询 控制层
 * 
 * 首页、全部课程、详细课程、员工-课程评价 页面控制层
 * 
 * @author 傅洁敏
 *
 */
@WebServlet("/CCPTServlet")
public class CCPTServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CCPTService cs = new CCPTServiceImpl();

	RefTableService rtsi = new RefTableServiceImp();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CCPTServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码 防止中文乱码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		// 获取动作
		String op = "";
		// 储存动作
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("queryByPage".equals(op)) {
			// 模糊查询+分页
			doQueryByPage(request, response);
		} else if ("queryByPage1".equals(op)) {
			// 类型相关课程2个
			doQueryByPage1(request, response);
		} else if ("queryByPage2".equals(op)) {
			// 类型相关课程2个
			doQueryByPage2(request, response);
		} else if ("queryByEva".equals(op)) {
			// 员工-课程评价 相关课程信息
			doQueryByEva(request, response);
		} else if ("queryByTea".equals(op)) {
			// 员工-课程评价 相关课程信息
			doQueryByTea(request, response);
		} else if ("queryByEmp".equals(op)) {
			// 员工-我的课程 相关课程信息
			doQueryByEmp(request, response);
		} else if ("ajaxReq".equals(op)) {
			// 获得所有课程类型
			doAjaxReq(request, response);
		} else if ("ajaxReq1".equals(op)) {
			// 获得最新课程
			doAjaxReq1(request, response);
		} else if ("queryByTeaPage".equals(op)) {
			doQueryByTeaPage(request, response);
		} else if ("teaQueryEndScoure".equals(op)) {
			// 教员查询结束的课程
			doTeaQueryEndScore(request, response);

		} else if ("queryByDeptPage".equals(op)) {
			//主管主页
			doQueryByDeptPage(request, response);
		} else if ("queryDeptCourse".equals(op)) {
			// 模糊查询+分页
			doQueryByPage3(request, response);
		}
	}

	private void doQueryByDeptPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得最新课程
		// 存储关键字
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为6
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByPage(page, pageSize, keywords);
		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);
		request.setAttribute("keywords", keywords);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("DeptMain.jsp").forward(request, response);
	}

	private void doTeaQueryEndScore(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 存储教员手机号
		String teacherTel = "";
		if (request.getParameter("teacherTel") != null) {
			teacherTel = request.getParameter("teacherTel");
		}
		// 存储课程状态
		int courseStatus = 3;

		// 页码默认为1
		int page = 1;
		// 每页记录数默认为6
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}

		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByTea(page, pageSize, teacherTel, courseStatus);
		// 获取上课实时人数
		int count = 0;
		for (int i = 0; i < pd.getData().size(); i++) {
			System.out.println("课程号:" + pd.getData().get(i).getCpNo());
			// 课程号
			int cpNo = pd.getData().get(i).getCpNo();
			System.out.println("cc" + cpNo);
			// 人数
			count = rtsi.getCountByCPNo(cpNo);
			System.out.println("人数：" + count);
			pd.getData().get(i).setCourseNowNum(count);
		}

		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);

		// 将课程状态传回到jsp页面上
		request.setAttribute("courseStatus", courseStatus);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("TeaEndCourse.jsp").forward(request, response);
	}

	private void doQueryByTeaPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得最新课程
		// 存储关键字
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为6
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByPage(page, pageSize, keywords);
		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);
		request.setAttribute("keywords", keywords);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("TeacherMain.jsp").forward(request, response);
	}

	/**
	 * 分页：员工-我的课程- 相关课程信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQueryByEmp(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 存储教员手机号
		String empTel = "";
		if (request.getParameter("empTel") != null) {
			empTel = request.getParameter("empTel");
		}
		// 存储课程状态
		String applyState = "0";
		if (request.getParameter("applyState") != null) {
			applyState = request.getParameter("applyState");
		}
		switch (applyState) {
		case "0":
			request.setAttribute("cs", "申请中");
			break;
		case "1":
			request.setAttribute("cs", "已通过");
			break;
		case "2":
			request.setAttribute("cs", "未通过");
			break;
		default:
			break;
		}
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为6
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByEmp(page, pageSize, empTel, applyState);
		// 获取上课实时人数
		int count = 0;
		for (int i = 0; i < pd.getData().size(); i++) {
			System.out.println("课程号:" + pd.getData().get(i).getCpNo());
			// 课程号
			int cpNo = pd.getData().get(i).getCpNo();
			System.out.println("cc" + cpNo);
			// 人数
			count = rtsi.getCountByCPNo(cpNo);
			System.out.println("人数：" + count);
			pd.getData().get(i).setCourseNowNum(count);
		}

		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);
		// 将课程状态传回到jsp页面上
		request.setAttribute("applyState", applyState);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("EmpCourseApl.jsp").forward(request, response);
	}

	private void doQueryByPage3(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 存储关键字
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为5
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByPage(page, pageSize, keywords);
		// 获取上课实时人数
		int count = 0;
		for (int i = 0; i < pd.getData().size(); i++) {
			System.out.println("课程号:" + pd.getData().get(i).getCpNo());
			// 课程号
			int cpNo = pd.getData().get(i).getCpNo();
			System.out.println("cc" + cpNo);
			// 人数
			count = rtsi.getCountByCPNo(cpNo);
			System.out.println("人数：" + count);
			pd.getData().get(i).setCourseNowNum(count);
		}
		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);
		request.setAttribute("keywords", keywords);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("DeptCoursesShow.jsp").forward(request, response);
	}

	/**
	 * 分页+查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 存储关键字
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为5
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByPage(page, pageSize, keywords);

		// 获取上课实时人数
		int count = 0;
		for (int i = 0; i < pd.getData().size(); i++) {
			System.out.println("课程号:" + pd.getData().get(i).getCpNo());
			// 课程号
			int cpNo = pd.getData().get(i).getCpNo();
			System.out.println("cc" + cpNo);
			// 人数
			count = rtsi.getCountByCPNo(cpNo);
			System.out.println("人数：" + count);
			pd.getData().get(i).setCourseNowNum(count);
		}

		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);
		request.setAttribute("keywords", keywords);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("BeforeCourses.jsp?").forward(request, response);
	}

	/**
	 * 分页+查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQueryByPage2(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 存储关键字
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为6
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByPage(page, pageSize, keywords);
		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);
		request.setAttribute("keywords", keywords);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("Main.jsp").forward(request, response);
	}

	/**
	 * 分页+查询:类型相关课程 2个
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQueryByPage1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 存储关键字
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为3,以免重复
		int pageSize = 3;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByPage(page, pageSize, keywords);
		List<CCPT> list = pd.getData();
		// 传递PageDate对象给JSP
		// 3将list转换为JSON格式的字符串
		Gson gson = new Gson();
		String listCourses = gson.toJson(list);
		// 4 响应
		PrintWriter out = response.getWriter();
		out.print(listCourses);
		// 5.关闭
		out.close();

	}

	/**
	 * 处理AJAX请求，获得所有课程类型
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doAjaxReq(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");
		// 2 调用 service的方法
		List<String> list = cs.getAllSubject();
		// 3将list转换为JSON格式的字符串
		Gson gson = new Gson();
		String listCourses = gson.toJson(list);
		// 4 响应
		PrintWriter out = response.getWriter();
		out.print(listCourses);
		// 5.关闭
		out.close();
	}

	/**
	 * 处理AJAX请求，获得4个最新课程
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doAjaxReq1(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");
		// 2 调用 service的方法
		List<CCPT> list = cs.getCPTInfo();
		// 3将list转换为JSON格式的字符串
		Gson gson = new Gson();
		String listCourses = gson.toJson(list);
		// 4 响应
		PrintWriter out = response.getWriter();
		out.print(listCourses);
		// 5.关闭
		out.close();
	}

	/**
	 * 分页：员工-课程评价 相关课程信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQueryByEva(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 存储关键字
		String empTel = "";
		if (request.getParameter("empTel") != null) {
			empTel = request.getParameter("empTel");
		}
		System.out.println(empTel);
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为6
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByEva(page, pageSize, empTel);
		// 获取上课实时人数
		int count = 0;
		for (int i = 0; i < pd.getData().size(); i++) {
			System.out.println("课程号:" + pd.getData().get(i).getCpNo());
			// 课程号
			int cpNo = pd.getData().get(i).getCpNo();
			System.out.println("cc" + cpNo);
			// 人数
			count = rtsi.getCountByCPNo(cpNo);
			System.out.println("人数：" + count);
			pd.getData().get(i).setCourseNowNum(count);
		}
		System.out.println(pd.getData());
		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("CourseEvaluation.jsp").forward(request, response);
	}

	/**
	 * 分页：教员- 相关课程信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQueryByTea(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 存储教员手机号
		String teacherTel = "";
		if (request.getParameter("teacherTel") != null) {
			teacherTel = request.getParameter("teacherTel");
		}
		// 存储课程状态
		int courseStatus = 1;
		if (request.getParameter("courseStatus") != null) {
			courseStatus = Integer.parseInt(request.getParameter("courseStatus"));
		}
		switch (courseStatus) {
		case 1:
			request.setAttribute("cs", "招募中");
			break;
		case 2:
			request.setAttribute("cs", "开课中");
			break;
		case 3:
			request.setAttribute("cs", "已结课");
			break;
		case 4:
			request.setAttribute("cs", "已取消");
			break;
		case 5:
			request.setAttribute("cs", "已满员");
			break;
		default:
			break;
		}
		// 页码默认为1
		int page = 1;
		// 每页记录数默认为6
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用Service方法
		PageData<CCPT> pd = cs.getCPTInfoByTea(page, pageSize, teacherTel, courseStatus);
		// 获取上课实时人数
		int count = 0;
		for (int i = 0; i < pd.getData().size(); i++) {
			System.out.println("课程号:" + pd.getData().get(i).getCpNo());
			// 课程号
			int cpNo = pd.getData().get(i).getCpNo();
			System.out.println("cc" + cpNo);
			// 人数
			count = rtsi.getCountByCPNo(cpNo);
			System.out.println("人数：" + count);
			pd.getData().get(i).setCourseNowNum(count);
		}
		// 传递PageDate对象给JSP
		request.setAttribute("pd", pd);
		// 将课程状态传回到jsp页面上
		request.setAttribute("courseStatus", courseStatus);
		// 跳转页面 转发 带request的参数
		request.getRequestDispatcher("TeacherCourse.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
